package com.andy.qhb.utils;

import java.util.Map;

import com.andy.qhb.entity.UsrGroups;
import com.andy.qhb.entity.UsrQiangbLog;
import com.andy.qhb.entity.UsrUser;
import com.andy.qhb.game.GameUser;
import com.andy.qhb.game.Global;
import com.andy.qhb.game.QhbGame;
import com.andy.qhb.game.Room;
import com.andy.qhb.websocket.MySocketSession;
import com.google.common.collect.Maps;

public class SendUtil {

	public static void send100(MySocketSession session,GameUser gameUser) {
		if(gameUser == null){
			Map<String,Object> map = Maps.newHashMap();
			map.put("respCode", 1);
			doSend(session,GmAction.ACTION_100, map);
		}else{
			Map<String, Object> map = gameUser.getUserInfo();
			map.put("respCode", GoConstant.SUCC_CODE);
			
			doSend(session,GmAction.ACTION_100, gameUser.getUserInfo());
		}
	}
	
	public static void send110(MySocketSession session,GameUser gameUser) {
		if(gameUser == null)return;
		Map<String,Object> map = Maps.newHashMap();
		map.put("flist", gameUser.getFriends().friends());
		map.put("glist", gameUser.getGroups());
		doSend(session,GmAction.ACTION_110, map);
	}
	
	public static void send111(MySocketSession myWebSocket, UsrUser usrUser) {
		doSend(myWebSocket,GmAction.ACTION_111, usrUser.forOther());
	}
	
	public static void send112(GameUser user, Long uid) {
		Map<String,Object> map = Maps.newHashMap();
		map.put("userId", uid);
		doSend(user,GmAction.ACTION_112, map);
	}
	
	public static void send113(MySocketSession myWebSocket, UsrGroups group) {
		doSend(myWebSocket,GmAction.ACTION_113, group);
	}
	
	public static void send114(MySocketSession myWebSocket, Long gid,UsrUser usrUser) {
		Map<String, Object> map = usrUser.forOther();
		map.put("groupId", gid);
		doSend(myWebSocket,GmAction.ACTION_114, map);
	}
	
	public static void send115(Room room, GameUser user) {
		if(room == null || user == null)return;
		Map<String,Object> map = Maps.newHashMap();
		map.put("userId", user.getId());
		sendRoom(room, GmAction.ACTION_115, map);
	}
	
	public static void send120(MySocketSession session,Room room) {
		Map<String,Object> map = Maps.newHashMap();
		map.put("roomId", room.getId());
		doSend(session,GmAction.ACTION_120, map);
	}
	
	public static void send121(Room room, QhbGame game) {
		Map<String,Object> map = Maps.newHashMap();
		map.put("pack", game);
		sendRoom(room, GmAction.ACTION_121, map);
	}
	
	public static void send122(Room room, UsrQiangbLog qiangbLog,String nn,String nn1) {
		Map<String,Object> map = Maps.newHashMap();
		map.put("packId", qiangbLog.getPid());
		map.put("nickName", nn);
		map.put("coin", qiangbLog.getCoin());
		map.put("nickName1", nn1);
		sendRoom(room, GmAction.ACTION_121, map);
	}
	
	public static void send128(GameUser user) {
		Map<String,Object> map = Maps.newHashMap();
		map.put("userId", user.getId());
		doSend(user,GmAction.ACTION_128, map);
	}
	
	public static void sendError(MySocketSession session,int action,String msg) {
		Map<String,Object> map = Maps.newHashMap();
		map.put("action", action);
		map.put("msg", msg);
		doSend(session,GmAction.ACTION_999, map);
	}
	
	
	public static void sendtoVar(Object sessionOrId,int action,Object... data) {
		if(data == null)return;
		Map<String,Object> map = Maps.newHashMap();
		for(int i=0;i<data.length;i+=2)
		map.put((String) data[i], data[i+1]);
		doSend(sessionOrId,action, map);
	}

	private static void doSend(Object sessionOrId,int action,Object data) {
		if(data == null)return;
		if(sessionOrId != null){
			MySocketSession sess = null;
			Map<String,Object> map = Maps.newHashMap();
			map.put("action", action);
			map.put("data", data);
			if(sessionOrId instanceof MySocketSession){
				sess = (MySocketSession) sessionOrId;
			}else if(sessionOrId instanceof GameUser){
				sess = Global.getSession(((GameUser) sessionOrId).getId());
			}else{
				sess = Global.getSession((Long) sessionOrId);
			}
			if(sess != null){
				sess.sendMessage(JsonUtils.object2JsonString(map));
			}
		}
	}
	
	public static void sendRoom(Room room,int action,Map<String,Object> data) {
		if(data == null)return;
		Map<String,Object> map = Maps.newHashMap();
		map.put("action", action);
		map.put("data", data);
		String msg = JsonUtils.object2JsonString(map);
		for(GameUser gameUser:room.users()){
			MySocketSession sess = Global.getSession(gameUser.getId());
			if(sess != null){
				sess.sendMessage(msg);
			}
		}
	}
	
	public static void broadcast(int action,String data) {
		if(data == null)return;
		for(MySocketSession session:Global.getUserSessions()){
			doSend(session,action,data);
		}
	}


}
